﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace AbetEvaluation
{
	public partial class TakeOutcome : System.Web.UI.Page
	{
		protected void Page_Load(object sender, EventArgs e)
		{

		}

		protected void btnSubmit_Click(object sender, EventArgs e)
		{
			decimal total = 0;
			decimal score = 0;

			//caculate score.
			foreach (GridViewRow row in gvQuestions.Rows)
			{
				bool answer = (row.FindControl("radioYes") as RadioButton).Checked;

				int points = (int)gvQuestions.DataKeys[row.RowIndex].Values[2];
				bool correct = (bool)gvQuestions.DataKeys[row.RowIndex].Values[1];

				total += points;
				if (answer == correct)
					score += points;
			}

			//insert take.
			Business.Take t = new Business.Take();
			t.ID = Guid.NewGuid();
			t.OutcomeID = Guid.Parse(Page.Request.QueryString["id"]);
			t.UserName = User.Identity.Name;
			t.Grade = score / total * 100m;
			Business.Taken.InsertTake(t);

			//insert rusults.
			foreach (GridViewRow row in gvQuestions.Rows)
			{
				Business.Result r = new Business.Result();
				r.TakenID = t.ID;
				r.QuestionID = (Guid)gvQuestions.DataKeys[row.RowIndex].Value;
				r.Answer = (row.FindControl("radioYes") as RadioButton).Checked;
				Business.Results.InsertResult(r);
			}

			//show user outcome results.
			Page.Response.Redirect("~/OutcomeResult.aspx?id=" + t.ID.ToString());
		}

		protected void btnClear_Click(object sender, EventArgs e)
		{
			gvQuestions.DataBind();
		}

		protected void gvQuestionsDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
		{
			e.InputParameters.Add("OutcomeID", Guid.Parse(Page.Request.QueryString["id"]));
		}
	}
}